﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Linq;
using CityLife.Objects;
using CityLife.CityService;
using System.Reflection;



namespace CityLife
{
    
    public class Zone : Canvas, ICityElement
    {
        UserControl cube = null;
        public Type CityElementDesign { get; set; }


        public List<Direction> ZoneDirections { get; set; }

        public Zone()
        {
            ZoneDirections = new List<Direction>();
            (this as Canvas).MouseLeftButtonUp += new MouseButtonEventHandler(Zone_MouseLeftButtonUp);
            CityElementDesign = typeof(Empty);           
        }
        public Zone(Type cityElementDesign)
        {
            ZoneDirections = new List<Direction>();
            (this as Canvas).MouseLeftButtonUp += new MouseButtonEventHandler(Zone_MouseLeftButtonUp);
            CityElementDesign = cityElementDesign;           
        }

        void Zone_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            MainPage.Editor.HandleCityClick(sender, e, true);
        }


        public long ConnectionID { get; set; }

        public DateTime CreateDate { get; set; }

        public DateTime UpdateDate { get; set; }

        public double X { get; set; }

        public double Y { get; set; }

        public int GridRow { get; set; }

        public int GridColumn { get; set; }

        public CityElementType ElementType { get; set; }

        public CityElementOrientation ElementOrientation { get; set; }

        public new UIElementCollection Children
        {
            get { return (this as Canvas).Children; }

        }


        public void Hilight()
        {
            this.Opacity = 0.5;
        }


        public void UnHilight()
        {
            this.Opacity = 1;
        }




        public ICityDesign Design
        {

            get
            {
                return cube as ICityDesign;
            }

        }

     

        public void init()
        {
            init(MainPage.Heart);
        }
        public void init(CityHeart cityheart)
        {
            if (this.ElementType  == CityElementType.Road)
            {

              cube = (UserControl)MainPage.Editor.SetRoad(this, 0);
                this.Tag = "ROAD";

            }
            else
            {
                if (CityElementDesign != null)
                {

                    cube = (UserControl)Assembly.GetExecutingAssembly().CreateInstance(CityElementDesign.FullName);
                    (cube as ICityDesign).SetBuildStep(this.ElementStage);
                }
                this.Children.Add(cube);
            }
        }

      

        #region ICityElement Members


        public int Capacity
        {
            get
            {
                if (this.Design != null)
                    return this.Design.Info.Capacity;
                else
                    return 0;
            }
            set { }
     
        }

        CityElementStage m_ElementStage = CityElementStage.None;

        public CityElementStage ElementStage
        {
            get { return m_ElementStage; }
            set { 
                
                
                m_ElementStage = value;
                if(cube != null)
                    (cube as ICityDesign).SetBuildStep(value);
            
            
            
            
            
            }
        }

       
      

        #endregion

        #region ICityElement Members


        public CursorType CursorType
        {
            get;
            set;
        }

        #endregion
    }
}